package com.heny.demo.service.impl;

import com.heny.demo.entity.TurnPoint;
import com.heny.demo.mapper.TurnPointMapper;
import com.heny.demo.service.FileImportService;
import com.heny.demo.strategy.FileParseStrategy;
import com.heny.demo.strategy.FileParseStrategyFactory;
import com.heny.demo.util.CoordinateTransformUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author :wushen
 * @since :2025/5/11 16:44
 */

@Service
public class FileImportServiceImpl implements FileImportService {

    @Autowired
    private FileParseStrategyFactory strategyFactory;

    @Autowired
    private TurnPointMapper turnPointMapper;

    @Override
    public List<TurnPoint> importFile(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        if (fileName == null) {
            throw new IllegalArgumentException("文件名为空");
        }
        String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        FileParseStrategy strategy = strategyFactory.getStrategy(fileExtension);
        List<TurnPoint> turnPoints = strategy.parseFile(file);

        // 坐标系转换
        CoordinateTransformUtil.transformPoints(turnPoints);

        // 使用batchInsert方法批量插入
        if (!turnPoints.isEmpty()) {
            turnPointMapper.batchInsert(turnPoints);
        }

        return turnPoints;
    }
}    